// Copyright (c) Lawrence Livermore National Security, LLC and other VisIt
// Project developers.  See the top-level LICENSE file for dates and other
// details.  No copyright assignment is required to contribute to VisIt.

#include <PyFileOpenOptions.h>
#include <ObserverToCallback.h>
#include <stdio.h>
#include <Py2and3Support.h>
#include <PyDBOptionsAttributes.h>

// ****************************************************************************
// Module: PyFileOpenOptions
//
// Purpose:
//   This class contains the file opening options for all the database plugins.
//
// Note:       Autogenerated by xml2python. Do not modify by hand!
//
// Programmer: xml2python
// Creation:   omitted
//
// ****************************************************************************

//
// This struct contains the Python type information and a FileOpenOptions.
//
struct FileOpenOptionsObject
{
    PyObject_HEAD
    FileOpenOptions *data;
    bool        owns;
    PyObject   *parent;
};

//
// Internal prototypes
//
static PyObject *NewFileOpenOptions(int);
std::string
PyFileOpenOptions_ToString(const FileOpenOptions *atts, const char *prefix, const bool forLogging)
{
    std::string str;
    char tmpStr[1000];

    {   const stringVector &typeNames = atts->GetTypeNames();
        snprintf(tmpStr, 1000, "%stypeNames = (", prefix);
        str += tmpStr;
        for(size_t i = 0; i < typeNames.size(); ++i)
        {
            snprintf(tmpStr, 1000, "\"%s\"", typeNames[i].c_str());
            str += tmpStr;
            if(i < typeNames.size() - 1)
            {
                snprintf(tmpStr, 1000, ", ");
                str += tmpStr;
            }
        }
        snprintf(tmpStr, 1000, ")\n");
        str += tmpStr;
    }
    {   const stringVector &typeIDs = atts->GetTypeIDs();
        snprintf(tmpStr, 1000, "%stypeIDs = (", prefix);
        str += tmpStr;
        for(size_t i = 0; i < typeIDs.size(); ++i)
        {
            snprintf(tmpStr, 1000, "\"%s\"", typeIDs[i].c_str());
            str += tmpStr;
            if(i < typeIDs.size() - 1)
            {
                snprintf(tmpStr, 1000, ", ");
                str += tmpStr;
            }
        }
        snprintf(tmpStr, 1000, ")\n");
        str += tmpStr;
    }
    { // new scope
        int index = 0;
        // Create string representation of openOptions from atts.
        for(AttributeGroupVector::const_iterator pos = atts->GetOpenOptions().begin(); pos != atts->GetOpenOptions().end(); ++pos, ++index)
        {
            const DBOptionsAttributes *current = (const DBOptionsAttributes *)(*pos);
            snprintf(tmpStr, 1000, "GetOpenOptions(%d).", index);
            std::string objPrefix(prefix + std::string(tmpStr));
            str += PyDBOptionsAttributes_ToString(current, objPrefix.c_str(), forLogging);
        }
        if(index == 0)
            str += "#openOptions does not contain any DBOptionsAttributes objects.\n";
    }
    {   const intVector &Enabled = atts->GetEnabled();
        snprintf(tmpStr, 1000, "%sEnabled = (", prefix);
        str += tmpStr;
        for(size_t i = 0; i < Enabled.size(); ++i)
        {
            snprintf(tmpStr, 1000, "%d", Enabled[i]);
            str += tmpStr;
            if(i < Enabled.size() - 1)
            {
                snprintf(tmpStr, 1000, ", ");
                str += tmpStr;
            }
        }
        snprintf(tmpStr, 1000, ")\n");
        str += tmpStr;
    }
    {   const stringVector &preferredIDs = atts->GetPreferredIDs();
        snprintf(tmpStr, 1000, "%spreferredIDs = (", prefix);
        str += tmpStr;
        for(size_t i = 0; i < preferredIDs.size(); ++i)
        {
            snprintf(tmpStr, 1000, "\"%s\"", preferredIDs[i].c_str());
            str += tmpStr;
            if(i < preferredIDs.size() - 1)
            {
                snprintf(tmpStr, 1000, ", ");
                str += tmpStr;
            }
        }
        snprintf(tmpStr, 1000, ")\n");
        str += tmpStr;
    }
    return str;
}

static PyObject *
FileOpenOptions_Notify(PyObject *self, PyObject *args)
{
    FileOpenOptionsObject *obj = (FileOpenOptionsObject *)self;
    obj->data->Notify();
    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
FileOpenOptions_SetTypeNames(PyObject *self, PyObject *args)
{
    FileOpenOptionsObject *obj = (FileOpenOptionsObject *)self;

    stringVector vec;

    if (PyUnicode_Check(args))
    {
        char const *val = PyUnicode_AsUTF8(args);
        std::string cval = std::string(val);
        if (val == 0 && PyErr_Occurred())
        {
            PyErr_Clear();
            return PyErr_Format(PyExc_TypeError, "arg not interpretable as C++ string");
        }
        vec.resize(1);
        vec[0] = cval;
    }
    else if (PySequence_Check(args))
    {
        vec.resize(PySequence_Size(args));
        for (Py_ssize_t i = 0; i < PySequence_Size(args); i++)
        {
            PyObject *item = PySequence_GetItem(args, i);

            if (!PyUnicode_Check(item))
            {
                Py_DECREF(item);
                return PyErr_Format(PyExc_TypeError, "arg %d is not a unicode string", (int) i);
            }

            char const *val = PyUnicode_AsUTF8(item);
            std::string cval = std::string(val);

            if (val == 0 && PyErr_Occurred())
            {
                Py_DECREF(item);
                PyErr_Clear();
                return PyErr_Format(PyExc_TypeError, "arg %d not interpretable as C++ string", (int) i);
            }
            Py_DECREF(item);

            vec[i] = cval;
        }
    }
    else
        return PyErr_Format(PyExc_TypeError, "arg(s) must be one or more string(s)");

    obj->data->GetTypeNames() = vec;
    // Mark the typeNames in the object as modified.
    obj->data->SelectTypeNames();

    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
FileOpenOptions_GetTypeNames(PyObject *self, PyObject *args)
{
    FileOpenOptionsObject *obj = (FileOpenOptionsObject *)self;
    // Allocate a tuple the with enough entries to hold the typeNames.
    const stringVector &typeNames = obj->data->GetTypeNames();
    PyObject *retval = PyTuple_New(typeNames.size());
    for(size_t i = 0; i < typeNames.size(); ++i)
        PyTuple_SET_ITEM(retval, i, PyString_FromString(typeNames[i].c_str()));
    return retval;
}

/*static*/ PyObject *
FileOpenOptions_SetTypeIDs(PyObject *self, PyObject *args)
{
    FileOpenOptionsObject *obj = (FileOpenOptionsObject *)self;

    stringVector vec;

    if (PyUnicode_Check(args))
    {
        char const *val = PyUnicode_AsUTF8(args);
        std::string cval = std::string(val);
        if (val == 0 && PyErr_Occurred())
        {
            PyErr_Clear();
            return PyErr_Format(PyExc_TypeError, "arg not interpretable as C++ string");
        }
        vec.resize(1);
        vec[0] = cval;
    }
    else if (PySequence_Check(args))
    {
        vec.resize(PySequence_Size(args));
        for (Py_ssize_t i = 0; i < PySequence_Size(args); i++)
        {
            PyObject *item = PySequence_GetItem(args, i);

            if (!PyUnicode_Check(item))
            {
                Py_DECREF(item);
                return PyErr_Format(PyExc_TypeError, "arg %d is not a unicode string", (int) i);
            }

            char const *val = PyUnicode_AsUTF8(item);
            std::string cval = std::string(val);

            if (val == 0 && PyErr_Occurred())
            {
                Py_DECREF(item);
                PyErr_Clear();
                return PyErr_Format(PyExc_TypeError, "arg %d not interpretable as C++ string", (int) i);
            }
            Py_DECREF(item);

            vec[i] = cval;
        }
    }
    else
        return PyErr_Format(PyExc_TypeError, "arg(s) must be one or more string(s)");

    obj->data->GetTypeIDs() = vec;
    // Mark the typeIDs in the object as modified.
    obj->data->SelectTypeIDs();

    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
FileOpenOptions_GetTypeIDs(PyObject *self, PyObject *args)
{
    FileOpenOptionsObject *obj = (FileOpenOptionsObject *)self;
    // Allocate a tuple the with enough entries to hold the typeIDs.
    const stringVector &typeIDs = obj->data->GetTypeIDs();
    PyObject *retval = PyTuple_New(typeIDs.size());
    for(size_t i = 0; i < typeIDs.size(); ++i)
        PyTuple_SET_ITEM(retval, i, PyString_FromString(typeIDs[i].c_str()));
    return retval;
}

/*static*/ PyObject *
FileOpenOptions_GetOpenOptions(PyObject *self, PyObject *args)
{
    FileOpenOptionsObject *obj = (FileOpenOptionsObject *)self;
    int index = -1;
    if (args == NULL)
        return PyErr_Format(PyExc_NameError, "Use .GetOpenOptions(int index) to get a single entry");
    if (!PyArg_ParseTuple(args, "i", &index))
        return PyErr_Format(PyExc_TypeError, "arg must be a single integer index");
    if (index < 0 || (size_t)index >= obj->data->GetOpenOptions().size())
        return PyErr_Format(PyExc_ValueError, "index out of range");

    // Since the new object will point to data owned by the this object,
    // we need to increment the reference count.
    Py_INCREF(self);

    PyObject *retval = PyDBOptionsAttributes_Wrap(&obj->data->GetOpenOptions(index));
    // Set the object's parent so the reference to the parent can be decref'd
    // when the child goes out of scope.
    PyDBOptionsAttributes_SetParent(retval, self);

    return retval;
}

PyObject *
FileOpenOptions_GetNumOpenOptions(PyObject *self, PyObject *args)
{
    FileOpenOptionsObject *obj = (FileOpenOptionsObject *)self;
    return PyInt_FromLong((long)obj->data->GetOpenOptions().size());
}

PyObject *
FileOpenOptions_AddOpenOptions(PyObject *self, PyObject *args)
{
    FileOpenOptionsObject *obj = (FileOpenOptionsObject *)self;
    PyObject *element = NULL;
    if(!PyArg_ParseTuple(args, "O", &element))
        return NULL;
    if(!PyDBOptionsAttributes_Check(element))
        return PyErr_Format(PyExc_TypeError, "expected attr object of type DBOptionsAttributes");
    DBOptionsAttributes *newData = PyDBOptionsAttributes_FromPyObject(element);
    obj->data->AddOpenOptions(*newData);
    obj->data->SelectOpenOptions();
    Py_INCREF(Py_None);
    return Py_None;
}

static PyObject *
FileOpenOptions_Remove_One_OpenOptions(PyObject *self, int index)
{
    FileOpenOptionsObject *obj = (FileOpenOptionsObject *)self;
    // Remove in the AttributeGroupVector instead of calling RemoveOpenOptions() because we don't want to delete the object; just remove it.
    AttributeGroupVector &atts = obj->data->GetOpenOptions();
    AttributeGroupVector::iterator pos = atts.begin();
    // Iterate through the vector "index" times.
    for(int i = 0; i < index; ++i)
        ++pos;

    // If pos is still a valid iterator, remove that element.
    if(pos != atts.end())
    {
        // NOTE: Leak the object since other Python objects may reference it. Ideally,
        // we would put the object into some type of pool to be cleaned up later but
        // this will do for now.
        //
        // delete *pos;
        atts.erase(pos);
    }

    obj->data->SelectOpenOptions();
    Py_INCREF(Py_None);
    return Py_None;
}

PyObject *
FileOpenOptions_RemoveOpenOptions(PyObject *self, PyObject *args)
{
    int index = -1;
    if(!PyArg_ParseTuple(args, "i", &index))
        return PyErr_Format(PyExc_TypeError, "Expecting integer index");
    FileOpenOptionsObject *obj = (FileOpenOptionsObject *)self;
    if(index < 0 || index >= obj->data->GetNumOpenOptions())
        return PyErr_Format(PyExc_IndexError, "Index out of range");

    return FileOpenOptions_Remove_One_OpenOptions(self, index);
}

PyObject *
FileOpenOptions_ClearOpenOptions(PyObject *self, PyObject *args)
{
    FileOpenOptionsObject *obj = (FileOpenOptionsObject *)self;
    int n = obj->data->GetNumOpenOptions();
    for(int i = 0; i < n; ++i)
    {
        FileOpenOptions_Remove_One_OpenOptions(self, 0);
        Py_DECREF(Py_None);
    }
    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
FileOpenOptions_SetEnabled(PyObject *self, PyObject *args)
{
    FileOpenOptionsObject *obj = (FileOpenOptionsObject *)self;

    intVector vec;

    if (PyNumber_Check(args))
    {
        long val = PyLong_AsLong(args);
        int cval = int(val);
        if (val == -1 && PyErr_Occurred())
        {
            PyErr_Clear();
            return PyErr_Format(PyExc_TypeError, "number not interpretable as C++ int");
        }
        if (fabs(double(val))>1.5E-7 && fabs((double(long(cval))-double(val))/double(val))>1.5E-7)
            return PyErr_Format(PyExc_ValueError, "number not interpretable as C++ int");
        vec.resize(1);
        vec[0] = cval;
    }
    else if (PySequence_Check(args) && !PyUnicode_Check(args))
    {
        vec.resize(PySequence_Size(args));
        for (Py_ssize_t i = 0; i < PySequence_Size(args); i++)
        {
            PyObject *item = PySequence_GetItem(args, i);

            if (!PyNumber_Check(item))
            {
                Py_DECREF(item);
                return PyErr_Format(PyExc_TypeError, "arg %d is not a number type", (int) i);
            }

            long val = PyLong_AsLong(item);
            int cval = int(val);

            if (val == -1 && PyErr_Occurred())
            {
                Py_DECREF(item);
                PyErr_Clear();
                return PyErr_Format(PyExc_TypeError, "arg %d not interpretable as C++ int", (int) i);
            }
            if (fabs(double(val))>1.5E-7 && fabs((double(long(cval))-double(val))/double(val))>1.5E-7)
            {
                Py_DECREF(item);
                return PyErr_Format(PyExc_ValueError, "arg %d not interpretable as C++ int", (int) i);
            }
            Py_DECREF(item);

            vec[i] = cval;
        }
    }
    else
        return PyErr_Format(PyExc_TypeError, "arg(s) must be one or more ints");

    obj->data->GetEnabled() = vec;
    // Mark the Enabled in the object as modified.
    obj->data->SelectEnabled();

    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
FileOpenOptions_GetEnabled(PyObject *self, PyObject *args)
{
    FileOpenOptionsObject *obj = (FileOpenOptionsObject *)self;
    // Allocate a tuple the with enough entries to hold the Enabled.
    const intVector &Enabled = obj->data->GetEnabled();
    PyObject *retval = PyTuple_New(Enabled.size());
    for(size_t i = 0; i < Enabled.size(); ++i)
        PyTuple_SET_ITEM(retval, i, PyInt_FromLong(long(Enabled[i])));
    return retval;
}

/*static*/ PyObject *
FileOpenOptions_SetPreferredIDs(PyObject *self, PyObject *args)
{
    FileOpenOptionsObject *obj = (FileOpenOptionsObject *)self;

    stringVector vec;

    if (PyUnicode_Check(args))
    {
        char const *val = PyUnicode_AsUTF8(args);
        std::string cval = std::string(val);
        if (val == 0 && PyErr_Occurred())
        {
            PyErr_Clear();
            return PyErr_Format(PyExc_TypeError, "arg not interpretable as C++ string");
        }
        vec.resize(1);
        vec[0] = cval;
    }
    else if (PySequence_Check(args))
    {
        vec.resize(PySequence_Size(args));
        for (Py_ssize_t i = 0; i < PySequence_Size(args); i++)
        {
            PyObject *item = PySequence_GetItem(args, i);

            if (!PyUnicode_Check(item))
            {
                Py_DECREF(item);
                return PyErr_Format(PyExc_TypeError, "arg %d is not a unicode string", (int) i);
            }

            char const *val = PyUnicode_AsUTF8(item);
            std::string cval = std::string(val);

            if (val == 0 && PyErr_Occurred())
            {
                Py_DECREF(item);
                PyErr_Clear();
                return PyErr_Format(PyExc_TypeError, "arg %d not interpretable as C++ string", (int) i);
            }
            Py_DECREF(item);

            vec[i] = cval;
        }
    }
    else
        return PyErr_Format(PyExc_TypeError, "arg(s) must be one or more string(s)");

    obj->data->GetPreferredIDs() = vec;
    // Mark the preferredIDs in the object as modified.
    obj->data->SelectPreferredIDs();

    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
FileOpenOptions_GetPreferredIDs(PyObject *self, PyObject *args)
{
    FileOpenOptionsObject *obj = (FileOpenOptionsObject *)self;
    // Allocate a tuple the with enough entries to hold the preferredIDs.
    const stringVector &preferredIDs = obj->data->GetPreferredIDs();
    PyObject *retval = PyTuple_New(preferredIDs.size());
    for(size_t i = 0; i < preferredIDs.size(); ++i)
        PyTuple_SET_ITEM(retval, i, PyString_FromString(preferredIDs[i].c_str()));
    return retval;
}



PyMethodDef PyFileOpenOptions_methods[FILEOPENOPTIONS_NMETH] = {
    {"Notify", FileOpenOptions_Notify, METH_VARARGS},
    {"SetTypeNames", FileOpenOptions_SetTypeNames, METH_VARARGS},
    {"GetTypeNames", FileOpenOptions_GetTypeNames, METH_VARARGS},
    {"SetTypeIDs", FileOpenOptions_SetTypeIDs, METH_VARARGS},
    {"GetTypeIDs", FileOpenOptions_GetTypeIDs, METH_VARARGS},
    {"GetOpenOptions", FileOpenOptions_GetOpenOptions, METH_VARARGS},
    {"GetNumOpenOptions", FileOpenOptions_GetNumOpenOptions, METH_VARARGS},
    {"AddOpenOptions", FileOpenOptions_AddOpenOptions, METH_VARARGS},
    {"RemoveOpenOptions", FileOpenOptions_RemoveOpenOptions, METH_VARARGS},
    {"ClearOpenOptions", FileOpenOptions_ClearOpenOptions, METH_VARARGS},
    {"SetEnabled", FileOpenOptions_SetEnabled, METH_VARARGS},
    {"GetEnabled", FileOpenOptions_GetEnabled, METH_VARARGS},
    {"SetPreferredIDs", FileOpenOptions_SetPreferredIDs, METH_VARARGS},
    {"GetPreferredIDs", FileOpenOptions_GetPreferredIDs, METH_VARARGS},
    {NULL, NULL}
};

//
// Type functions
//

static void
FileOpenOptions_dealloc(PyObject *v)
{
   FileOpenOptionsObject *obj = (FileOpenOptionsObject *)v;
   if(obj->parent != 0)
       Py_DECREF(obj->parent);
   if(obj->owns)
       delete obj->data;
}

static PyObject *FileOpenOptions_richcompare(PyObject *self, PyObject *other, int op);
PyObject *
PyFileOpenOptions_getattr(PyObject *self, char *name)
{
    if(strcmp(name, "typeNames") == 0)
        return FileOpenOptions_GetTypeNames(self, NULL);
    if(strcmp(name, "typeIDs") == 0)
        return FileOpenOptions_GetTypeIDs(self, NULL);
    if(strcmp(name, "openOptions") == 0)
        return FileOpenOptions_GetOpenOptions(self, NULL);
    if(strcmp(name, "Enabled") == 0)
        return FileOpenOptions_GetEnabled(self, NULL);
    if(strcmp(name, "preferredIDs") == 0)
        return FileOpenOptions_GetPreferredIDs(self, NULL);


    // Add a __dict__ answer so that dir() works
    if (!strcmp(name, "__dict__"))
    {
        PyObject *result = PyDict_New();
        for (int i = 0; PyFileOpenOptions_methods[i].ml_meth; i++)
            PyDict_SetItem(result,
                PyString_FromString(PyFileOpenOptions_methods[i].ml_name),
                PyString_FromString(PyFileOpenOptions_methods[i].ml_name));
        return result;
    }

    return Py_FindMethod(PyFileOpenOptions_methods, self, name);
}

int
PyFileOpenOptions_setattr(PyObject *self, char *name, PyObject *args)
{
    PyObject NULL_PY_OBJ;
    PyObject *obj = &NULL_PY_OBJ;

    if(strcmp(name, "typeNames") == 0)
        obj = FileOpenOptions_SetTypeNames(self, args);
    else if(strcmp(name, "typeIDs") == 0)
        obj = FileOpenOptions_SetTypeIDs(self, args);
    else if(strcmp(name, "Enabled") == 0)
        obj = FileOpenOptions_SetEnabled(self, args);
    else if(strcmp(name, "preferredIDs") == 0)
        obj = FileOpenOptions_SetPreferredIDs(self, args);

    if (obj != NULL && obj != &NULL_PY_OBJ)
        Py_DECREF(obj);

    if (obj == &NULL_PY_OBJ)
    {
        obj = NULL;
        PyErr_Format(PyExc_NameError, "name '%s' is not defined", name);
    }
    else if (obj == NULL && !PyErr_Occurred())
        PyErr_Format(PyExc_RuntimeError, "unknown problem with '%s'", name);

    return (obj != NULL) ? 0 : -1;
}

static int
FileOpenOptions_print(PyObject *v, FILE *fp, int flags)
{
    FileOpenOptionsObject *obj = (FileOpenOptionsObject *)v;
    fprintf(fp, "%s", PyFileOpenOptions_ToString(obj->data, "",false).c_str());
    return 0;
}

PyObject *
FileOpenOptions_str(PyObject *v)
{
    FileOpenOptionsObject *obj = (FileOpenOptionsObject *)v;
    return PyString_FromString(PyFileOpenOptions_ToString(obj->data,"", false).c_str());
}

//
// The doc string for the class.
//
#if PY_MAJOR_VERSION > 2 || (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION >= 5)
static const char *FileOpenOptions_Purpose = "This class contains the file opening options for all the database plugins.";
#else
static char *FileOpenOptions_Purpose = "This class contains the file opening options for all the database plugins.";
#endif

//
// Python Type Struct Def Macro from Py2and3Support.h
//
//         VISIT_PY_TYPE_OBJ( VPY_TYPE,
//                            VPY_NAME,
//                            VPY_OBJECT,
//                            VPY_DEALLOC,
//                            VPY_PRINT,
//                            VPY_GETATTR,
//                            VPY_SETATTR,
//                            VPY_STR,
//                            VPY_PURPOSE,
//                            VPY_RICHCOMP,
//                            VPY_AS_NUMBER)

//
// The type description structure
//

VISIT_PY_TYPE_OBJ(FileOpenOptionsType,         \
                  "FileOpenOptions",           \
                  FileOpenOptionsObject,       \
                  FileOpenOptions_dealloc,     \
                  FileOpenOptions_print,       \
                  PyFileOpenOptions_getattr,   \
                  PyFileOpenOptions_setattr,   \
                  FileOpenOptions_str,         \
                  FileOpenOptions_Purpose,     \
                  FileOpenOptions_richcompare, \
                  0); /* as_number*/

//
// Helper function for comparing.
//
static PyObject *
FileOpenOptions_richcompare(PyObject *self, PyObject *other, int op)
{
    // only compare against the same type 
    if ( Py_TYPE(self) != &FileOpenOptionsType
         || Py_TYPE(other) != &FileOpenOptionsType)
    {
        Py_INCREF(Py_NotImplemented);
        return Py_NotImplemented;
    }

    PyObject *res = NULL;
    FileOpenOptions *a = ((FileOpenOptionsObject *)self)->data;
    FileOpenOptions *b = ((FileOpenOptionsObject *)other)->data;

    switch (op)
    {
       case Py_EQ:
           res = (*a == *b) ? Py_True : Py_False;
           break;
       case Py_NE:
           res = (*a != *b) ? Py_True : Py_False;
           break;
       default:
           res = Py_NotImplemented;
           break;
    }

    Py_INCREF(res);
    return res;
}

//
// Helper functions for object allocation.
//

static FileOpenOptions *defaultAtts = 0;
static FileOpenOptions *currentAtts = 0;

static PyObject *
NewFileOpenOptions(int useCurrent)
{
    FileOpenOptionsObject *newObject;
    newObject = PyObject_NEW(FileOpenOptionsObject, &FileOpenOptionsType);
    if(newObject == NULL)
        return NULL;
    if(useCurrent && currentAtts != 0)
        newObject->data = new FileOpenOptions(*currentAtts);
    else if(defaultAtts != 0)
        newObject->data = new FileOpenOptions(*defaultAtts);
    else
        newObject->data = new FileOpenOptions;
    newObject->owns = true;
    newObject->parent = 0;
    return (PyObject *)newObject;
}

static PyObject *
WrapFileOpenOptions(const FileOpenOptions *attr)
{
    FileOpenOptionsObject *newObject;
    newObject = PyObject_NEW(FileOpenOptionsObject, &FileOpenOptionsType);
    if(newObject == NULL)
        return NULL;
    newObject->data = (FileOpenOptions *)attr;
    newObject->owns = false;
    newObject->parent = 0;
    return (PyObject *)newObject;
}

///////////////////////////////////////////////////////////////////////////////
//
// Interface that is exposed to the VisIt module.
//
///////////////////////////////////////////////////////////////////////////////

PyObject *
FileOpenOptions_new(PyObject *self, PyObject *args)
{
    int useCurrent = 0;
    if (!PyArg_ParseTuple(args, "i", &useCurrent))
    {
        if (!PyArg_ParseTuple(args, ""))
            return NULL;
        else
            PyErr_Clear();
    }

    return (PyObject *)NewFileOpenOptions(useCurrent);
}

//
// Plugin method table. These methods are added to the visitmodule's methods.
//
static PyMethodDef FileOpenOptionsMethods[] = {
    {"FileOpenOptions", FileOpenOptions_new, METH_VARARGS},
    {NULL,      NULL}        /* Sentinel */
};

static Observer *FileOpenOptionsObserver = 0;

std::string
PyFileOpenOptions_GetLogString()
{
    std::string s("FileOpenOptions = FileOpenOptions()\n");
    if(currentAtts != 0)
        s += PyFileOpenOptions_ToString(currentAtts, "FileOpenOptions.", true);
    return s;
}

static void
PyFileOpenOptions_CallLogRoutine(Subject *subj, void *data)
{
    typedef void (*logCallback)(const std::string &);
    logCallback cb = (logCallback)data;

    if(cb != 0)
    {
        std::string s("FileOpenOptions = FileOpenOptions()\n");
        s += PyFileOpenOptions_ToString(currentAtts, "FileOpenOptions.", true);
        cb(s);
    }
}

void
PyFileOpenOptions_StartUp(FileOpenOptions *subj, void *data)
{
    if(subj == 0)
        return;

    currentAtts = subj;
    PyFileOpenOptions_SetDefaults(subj);

    //
    // Create the observer that will be notified when the attributes change.
    //
    if(FileOpenOptionsObserver == 0)
    {
        FileOpenOptionsObserver = new ObserverToCallback(subj,
            PyFileOpenOptions_CallLogRoutine, (void *)data);
    }

}

void
PyFileOpenOptions_CloseDown()
{
    delete defaultAtts;
    defaultAtts = 0;
    delete FileOpenOptionsObserver;
    FileOpenOptionsObserver = 0;
}

PyMethodDef *
PyFileOpenOptions_GetMethodTable(int *nMethods)
{
    *nMethods = 1;
    return FileOpenOptionsMethods;
}

bool
PyFileOpenOptions_Check(PyObject *obj)
{
    return (obj->ob_type == &FileOpenOptionsType);
}

FileOpenOptions *
PyFileOpenOptions_FromPyObject(PyObject *obj)
{
    FileOpenOptionsObject *obj2 = (FileOpenOptionsObject *)obj;
    return obj2->data;
}

PyObject *
PyFileOpenOptions_New()
{
    return NewFileOpenOptions(0);
}

PyObject *
PyFileOpenOptions_Wrap(const FileOpenOptions *attr)
{
    return WrapFileOpenOptions(attr);
}

void
PyFileOpenOptions_SetParent(PyObject *obj, PyObject *parent)
{
    FileOpenOptionsObject *obj2 = (FileOpenOptionsObject *)obj;
    obj2->parent = parent;
}

void
PyFileOpenOptions_SetDefaults(const FileOpenOptions *atts)
{
    if(defaultAtts)
        delete defaultAtts;

    defaultAtts = new FileOpenOptions(*atts);
}

